package com.example.ad.sdk.domain.helper

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context

/**
 * 用于管理应用级别的全局状态和资源。
 */
@SuppressLint("StaticFieldLeak")
object ContextHelper {

    // 应用实例，用于全局访问
    private var _application: Application? = null
    // 应用上下文，用于全局访问
    private var _context: Context? = null

    /**
     * 初始化
     *
     * @param application 应用实例，用于初始化应用上下文
     */
//    fun init(application: Application){
//        _application = application
//        _context = application.applicationContext
//    }
    fun init(context: Context){
        _application = context.applicationContext as Application?
        _context = context
    }

    /**
     * 获取全局上下文
     *
     * @return Context 全局应用上下文
     * @throws NullPointerException 如果在调用此方法前未调用init方法初始化
     */
    val context: Context
        get() {
            if (_context == null) throw NullPointerException("u should init first")
            return _context as Context
        }

    /**
     * 获取应用实例
     *
     * @return Application 当前应用实例
     * @throws NullPointerException 如果在调用此方法前未调用init方法初始化
     */
    val application: Application
        get() {
            if (_application == null) throw NullPointerException("u should init first")
            return _application as Application
        }
}